package com.guinea.svw.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.guinea.svw.dao.SvwJdbcDao;
import com.guinea.svw.entity.IEntry;

public class CommonManager {
	public final String CHECKED = "<w:sym w:font=\"Wingdings\" w:char=\"F0FE\"/>";
	public final String NOTCHECKED = "<w:sym w:font=\"Wingdings\" w:char=\"F0A8\"/>";
	SvwJdbcDao svwJdbcDao;

	public SvwJdbcDao getSvwJdbcDao() {
		return svwJdbcDao;
	}
	public void setSvwJdbcDao(SvwJdbcDao svwJdbcDao) {
		this.svwJdbcDao = svwJdbcDao;
	}
	public List getSvwUsers(){
		return svwJdbcDao.getAllUsers();
	}
	public List getProvinces(){
		return svwJdbcDao.getProvinces();
	}
	public List getCities(){
		return svwJdbcDao.getCities();
	}
	public List getDistricts(){
		return svwJdbcDao.getDistricts();
	}
	public String convertPCDToJson(List provinces,List cities, List districts){
		cities = assembleEntry(cities,districts);
		provinces = assembleEntry(provinces,cities);
		return convertEntryListToJson(provinces);
	}
	/**字典项转成JSON数据格式
	 * @param entryList
	 * @return
	 */
	public String convertEntryListToJson(List entryList) {
		if(entryList == null) return null;
		String result = "[";
		for (Iterator iter = entryList.iterator(); iter.hasNext();) {
			IEntry e = (IEntry) iter.next();
			if(StringUtils.isBlank(e.getCode())) continue;
			result += "{";
			result += "\"code\" : \"" + e.getCode() + "\"";
			result += ", \"name\" : \"" + e.getName() + "\"";
			if(!StringUtils.isBlank(e.getId()))
				result += ", \"id\" : \"" + e.getId() + "\"";
			if(!StringUtils.isBlank(e.getParentId()))
				result += ", \"parentId\" : \"" + e.getParentId() + "\"";
			if (e.getList() != null && e.getList().size() > 0)
				result += ", \"list\" : " + convertEntryListToJson(e.getList());
			result += "}";
			if (iter.hasNext())
				result += ", ";
		}
		result += "]";
		return result;
	}
	
	/**
	 * 装配父条目和子条目
	 * 
	 * @param parents
	 * @param children
	 * @return
	 */
	public List assembleEntry(List parents,List children){
		if(children == null) return parents;
		Object[] x = children.toArray();
		Object[] y = parents.toArray();
		for (int i = 0; i < x.length; i++) {
			IEntry entry = (IEntry)x[i];
			if(StringUtils.isBlank(entry.getId()) || StringUtils.isBlank(entry.getParentId())){
				//System.out.println("i="+i);
				continue;
			} 
			IEntry parentEntry = getEntryById(entry.getParentId(),y);
			if(parentEntry == null) continue;
			if(parentEntry.getList() == null)parentEntry.setList(new ArrayList());
			parentEntry.getList().add(entry);
		}
		return parents;
	}
	/**根据id找到字典条目
	 * @param id
	 * @param entryList
	 * @return
	 */
	public IEntry getEntryById(String id,Object[] entries){
		for (int i = 0; i < entries.length; i++) {
			IEntry e = (IEntry)entries[i];
			if(id.equals(e.getId())) return e;
		}
		return null;
	}
	
	public String getSerial(String provinceId){
		return "S-" + provinceId + "-" + getSvwJdbcDao().getSerialNext(provinceId);
	}
//	/**取得所有申请word格式的数据
//	 * @return
//	 */
//	public List getApplicationWords(){
//		List aids = svwJdbcDao.getApplicationIds();
//		List applications = svwJdbcDao.getApplicationsRaw();
//		List applicationWords = new ArrayList();
//		List provinces = getProvinces();
//		List cities = getCities();
//		List districts = getDistricts();
//		for (Iterator iter = applications.iterator(); iter.hasNext();) {
//			Application application = (Application) iter.next();
//			//找到applicationId
//			String applicationId = null;
//			for (Iterator iterator = aids.iterator(); iterator
//					.hasNext();) {
//				String aid = (String) iterator.next();
//				if(new Long(aid).longValue() == application.getId().longValue()){
//					applicationId = aid;
//				}
//			}
//			if(applicationId == null) continue;
//			//按照applicationId查找其它内容
//			List other4s = getSvwJdbcDao().getOther4sByApplicationId(applicationId);
//			List regions = getSvwJdbcDao().getRegionsByApplicationId(applicationId);
//			for (Iterator iter2 = regions.iterator(); iter2.hasNext();) {
//				Region region = (Region) iter2.next();
//				if(region.getTownId().longValue() == 0L){
//					region.setProvinceId(application.getLocusProvinceId());
//					region.setCityId(application.getLocusCityId());
//					region.setDistrictId(application.getLocusDistrictId());
//				};
//			}
//			application.setOther4s(other4s);
//			application.setRegions(regions);
//			//经济性质5项改为4项
//			if("5".equals(application.getEconomicNature())){
//				application.setEconomicNature("1");
//			}
//			
//			if(application == null) application = new Application();
//			ApplicationWordVo applicationWordVo = null;
//			try {
//				applicationWordVo = applicationToWord(application,provinces,cities,districts);
//			} catch (IllegalAccessException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (InvocationTargetException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			if(applicationWordVo == null) applicationWordVo = new ApplicationWordVo();
//			applicationWords.add(applicationWordVo);
//		}
//		
//		return applicationWords;
//	}
	
	
//	/**根据申请id取得申请word格式的数据
//	 * @param applicationId
//	 * @return
//	 */
//	public ApplicationWordVo getApplicationWordById(String applicationId){
//		List al = svwJdbcDao.getApplicationById(applicationId);
//		Application application = null;
//		if(al.size()>0){
//			application = (Application) al.get(0);
//			List other4s = getSvwJdbcDao().getOther4sByApplicationId(applicationId);
//			List regions = getSvwJdbcDao().getRegionsByApplicationId(applicationId);
//			for (Iterator iter = regions.iterator(); iter.hasNext();) {
//				Region region = (Region) iter.next();
//				if(region.getTownId().longValue() == 0L){
//					region.setProvinceId(application.getLocusProvinceId());
//					region.setCityId(application.getLocusCityId());
//					region.setDistrictId(application.getLocusDistrictId());
//				};
//			}
//			application.setOther4s(other4s);
//			application.setRegions(regions);
//			//经济性质5项改为4项
//			if("5".equals(application.getEconomicNature())){
//				application.setEconomicNature("1");
//			}
//		} 
//		if(application == null) application = new Application();
//		List provinces = getProvinces();
//		List cities = getCities();
//		List districts = getDistricts();
//		ApplicationWordVo applicationWordVo = null;
//		try {
//			applicationWordVo = applicationToWord(application,provinces,cities,districts);
//		} catch (IllegalAccessException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (InvocationTargetException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		if(applicationWordVo == null) applicationWordVo = new ApplicationWordVo();
//		return applicationWordVo;
//	}
//	
//	/**申请数据转成WORD格式
//	 * @param application
//	 * @param provinces
//	 * @param cities
//	 * @param districts
//	 * @return
//	 * @throws IllegalAccessException
//	 * @throws InvocationTargetException
//	 */
//	public ApplicationWordVo applicationToWord(Application application,List provinces,List cities,List districts) throws IllegalAccessException, InvocationTargetException{
//		ApplicationWordVo result = new ApplicationWordVo();
//		BeanUtils.copyProperties(result,application);
//		
//		if (application.getLocusProvinceId() != null)
//			for (Iterator iter = provinces.iterator(); iter.hasNext();) {
//				Province province = (Province) iter.next();
//				if (province.getCode() != null
//						&& province.getCode().equals(
//								application.getLocusProvinceId())) {
//					result.setLocusProvince(province.getAbbr());
//				}
//			}
//		if (application.getLocusCityId() != null)
//			for (Iterator iter = cities.iterator(); iter.hasNext();) {
//				City city = (City) iter.next();
//				if (city.getCode() != null
//						&& city.getCode().equals(
//								application.getLocusCityId())) {
//					result.setLocusCity(city.getName());
//				}
//			}
//		if (application.getLocusDistrictId() != null)
//			for (Iterator iter = districts.iterator(); iter.hasNext();) {
//				District district = (District) iter.next();
//				if (district.getCode() != null
//						&& district.getCode().equals(
//								application.getLocusDistrictId())) {
//					result.setLocusDistrict(district.getName());
//				}
//			}
//		
//		String[] economicNatureOptions = new String[4];
//		if(application.getEconomicNature() != null){
//			for(int i=0;i<economicNatureOptions.length;i++)
//				economicNatureOptions[i]=NOTCHECKED;
//			int checkNum = Integer.parseInt(application.getEconomicNature());
//			if(checkNum > 0 && checkNum < 5)
//				economicNatureOptions[checkNum-1]=CHECKED;
//		}
//		result.setEconomicNatureOptions(economicNatureOptions);
//		
//		String isJointStock = NOTCHECKED;
//		if(application.getIsJointStock() != null && application.getIsJointStock().equals("1"))
//			isJointStock = CHECKED;
//		result.setIsJointStock(isJointStock);
//		
//		if (application.getContactProvinceId() != null)
//			for (Iterator iter = provinces.iterator(); iter.hasNext();) {
//				Province province = (Province) iter.next();
//				if (province.getCode() != null
//						&& province.getCode().equals(
//								application.getContactProvinceId())) {
//					result.setContactProvince(province.getAbbr());
//				}
//			}
//		if (application.getContactCityId() != null)
//			for (Iterator iter = cities.iterator(); iter.hasNext();) {
//				City city = (City) iter.next();
//				if (city.getCode() != null
//						&& city.getCode().equals(
//								application.getContactCityId())) {
//					result.setContactCity(city.getName());
//				}
//			}
//		if (application.getContactDistrictId() != null)
//			for (Iterator iter = districts.iterator(); iter.hasNext();) {
//				District district = (District) iter.next();
//				if (district.getCode() != null
//						&& district.getCode().equals(
//								application.getContactDistrictId())) {
//					result.setContactDistrict(district.getName());
//				}
//			}
//		
//		if(application.getBeginSalesMonth() != null && application.getBeginSalesYear() != null)
//			result.setBeginSalesMonthYear(application.getBeginSalesMonth() + "/" + application.getBeginSalesYear());
//		
//		if(application.getBeginServiceMonth() != null && application.getBeginServiceYear() != null)
//			result.setBeginServiceMonthYear(application.getBeginServiceMonth() + "/" + application.getBeginServiceYear());
//		
//		String hasOther4s = NOTCHECKED;
//		if(application.getHasOther4s() != null && application.getHasOther4s().equals("1"))
//			hasOther4s = CHECKED;
//		result.setHasOther4s(hasOther4s);
//		
//		if(application.getLastYear() != null && application.getLastYear().length()>2)
//			 result.setLastYear(application.getLastYear().substring(application.getLastYear().length()-2));
//		
//		if(application.getLastFinanceYear() != null && application.getLastFinanceYear().length()>2)
//			 result.setLastFinanceYear(application.getLastFinanceYear().substring(application.getLastFinanceYear().length()-2));
//		
//		String has2handLicense = NOTCHECKED;
//		if(application.getHas2handLicense() != null && application.getHas2handLicense().equals("1"))
//			has2handLicense = CHECKED;
//		result.setHas2handLicense(has2handLicense);
//		
//		String isRental = NOTCHECKED;
//		if(application.getIsRental() != null && application.getIsRental().equals("1"))
//			isRental = CHECKED;
//		result.setIsRental(isRental);
//		
//		if(application.getPlanToSignMonth() != null && application.getPlanToSignYear() != null)
//			result.setPlanToSignMonthYear(application.getPlanToSignMonth() + "/" + application.getPlanToSignYear());
//		
//		if(application.getPlanToStartMonth() != null && application.getPlanToStartYear() != null)
//			result.setPlanToStartMonthYear(application.getPlanToStartMonth() + "/" + application.getPlanToStartYear());
//		
//		if(application.getPlanToFinishMonth() != null && application.getPlanToFinishYear() != null)
//			result.setPlanToFinishMonthYear(application.getPlanToFinishMonth() + "/" + application.getPlanToFinishYear());
//		
//		//other4s
//		List other4sList = new ArrayList();
//		if(application.getOther4s() != null){
//			for (Iterator iter = application.getOther4s().iterator(); iter.hasNext();) {
//				Other4s other4s = (Other4s) iter.next();
//				Other4sWordVo  other4sWordVo = new Other4sWordVo();
//				BeanUtils.copyProperties(other4sWordVo,other4s);
//				if(other4s.getStartingTime() != null){
//					 DateTimeFormatter fmtSrc = DateTimeFormat.forPattern("yyyyMMdd");
//					 DateTimeFormatter fmtDest = DateTimeFormat.forPattern("yyyy-MM-dd");
//					 DateTime dt = fmtSrc.parseDateTime(other4s.getStartingTime());
//					 String dest = fmtDest.print(dt);
//					 other4sWordVo.setStartingTime(dest);
//				}
//				other4sList.add(other4sWordVo);
//			}
//		}
//		result.setOther4s(other4sList);
//		
//		//regions
//		List regions = new ArrayList();
//		if(application.getRegions() != null){
//			for (Iterator iter = application.getRegions().iterator(); iter.hasNext();) {
//				Region region = (Region) iter.next();
//				RegionWordVo regionWordVo = new RegionWordVo();
//				BeanUtils.copyProperties(regionWordVo,region);
//				
//				if (region.getProvinceId() != null)
//					for (Iterator iter2 = provinces.iterator(); iter2.hasNext();) {
//						Province province = (Province) iter2.next();
//						if (province.getCode() != null
//								&& province.getCode().equals(
//										region.getProvinceId())) {
//							regionWordVo.setProvince(province.getAbbr());
//						}
//					}
//				if (region.getCityId() != null)
//					for (Iterator iter2 = cities.iterator(); iter2.hasNext();) {
//						City city = (City) iter2.next();
//						if (city.getCode() != null
//								&& city.getCode().equals(
//										region.getCityId())) {
//							regionWordVo.setCity(city.getName());
//						}
//					}
//				if (region.getDistrictId() != null)
//					for (Iterator iter2 = districts.iterator(); iter2.hasNext();) {
//						District district = (District) iter2.next();
//						if (district.getCode() != null
//								&& district.getCode().equals(
//										region.getDistrictId())) {
//							regionWordVo.setDistrict(district.getName());
//						}
//					}
//				
//				String[] method = new String[3];
//				if(region.getMethodCode() != null){
//					for(int i=0;i<method.length;i++)
//						method[i]=NOTCHECKED;
//					int checkNum = Integer.parseInt(region.getMethodCode());
//					if(checkNum > 0 && checkNum < 4)
//						method[checkNum-1]=CHECKED;
//				}
//				regionWordVo.setMethod(method);
//				
//				String[] power = new String[3];
//				if(region.getPowerCode() != null){
//					for(int i=0;i<power.length;i++)
//						power[i]=NOTCHECKED;
//					int checkNum = Integer.parseInt(region.getPowerCode());
//					if(checkNum > 0 && checkNum < 4)
//						power[checkNum-1]=CHECKED;
//				}
//				regionWordVo.setPower(power);
//				
//				if(region.getAcceptMonth() != null && region.getAcceptYear() != null)
//					regionWordVo.setAcceptMonthYear(region.getAcceptMonth() + "/" + region.getAcceptYear());
//				
//				regions.add(regionWordVo);
//			}
//		}
//		result.setRegions(regions);
//		
//		return result;
//	}
	
	public void exportUsersFromSvw(){
		List svwUsers = svwJdbcDao.getAllUsers();
	}
}
